package com.example.myapplication.notification;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

import com.example.myapplication.R;

public class PushActivity extends AppCompatActivity {
    private static final String TAG = "ReceiverManager";
    private static final String PKG_NAME = "com.example.myapplication";
    private static final String CLS = "com.example.myapplication.notification.PushService";
    private static final String CHANNEL_ID = "vehicleInfoId";
    private static final String CHANNEL_NAME = "vehicleInfoName";
    private static final String GROUP = PKG_NAME;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_push);
    }

    public void show(View view) {
        sendNotification(this);
    }

    public void cancel(View view) {
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        for (int i = 100; i < 105; i++) {
            notificationManager.cancel(i);
        }
    }


    public void sendNotification(Context context) {
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
        for (int i = 100; i < 110; i++) {
            notificationManager.notify(i, createNotification(i, context));
        }
        notificationManager.notify(20, createNotification(20, context));
        notificationManager.notify(120, createNotification(120, context));
    }

    private Notification createNotification(int sortKey, Context context) {
        Bundle bundle = new Bundle();
        bundle.putInt("patac_notification_type", 0);
//        PendingIntent lbsIntent = getLbsIntent(context, entity);
        //创建channel
        createNotificationChannel(context);
        NotificationCompat.Builder notification = new NotificationCompat.Builder(context, CHANNEL_ID)
                .setContentTitle("123")
                .setContentText("额呵呵")
                .setSmallIcon(R.mipmap.ic_launcher)
                .addAction(new NotificationCompat.Action(0,
                        "详情" + sortKey, getDetailIntent(context)))
                .addAction(new NotificationCompat.Action(0,
                        "百度", getLBSIntent(context)))
                .addExtras(bundle)
                .setSortKey(sortKey + "")
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                //.setGroup(GROUP)
                .setAutoCancel(true);
//        if (entity.getBtnStrTwo() != null) {
//            //需要跳转到详情
//            notification.addAction(new NotificationCompat.Action(1, entity.getBtnStrTwo(),
//                    getDetailIntent(context, entity)));
//        }
        return notification.build();
    }

    private void createNotificationChannel(Context context) {
        NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME,
                NotificationManager.IMPORTANCE_HIGH);
        manager.createNotificationChannel(channel);
    }

    private PendingIntent getDetailIntent(Context context) {
        Intent intent = new Intent();
        intent.putExtra("KEY_TYPE", 3);
        intent.putExtra("KEY_ACTION_ID", 30);
        //再次回调起PushService,执行按钮跳转到详情
        ComponentName componentName = new ComponentName(PKG_NAME, CLS);
        intent.setComponent(componentName);
        return PendingIntent.getService(context, 1, intent, 0);
    }

    private PendingIntent getLBSIntent(Context context) {
        Intent intent = new Intent();
        intent.putExtra("KEY_TYPE", 2);
        intent.putExtra("KEY_ACTION_ID", 20);
        //再次回调起PushService,执行按钮跳转到详情
        ComponentName componentName = new ComponentName(PKG_NAME, CLS);
        intent.setComponent(componentName);
        return PendingIntent.getService(context, 2, intent, 0);
    }
}
